$(function() {
	//设置活动按钮
	changeActiveItem('newsList');
	loadCate();
	//初始化列表数据
	initTable();
	savehandle();
});

function loadCate() {
	$.getJSON('/fw/news/news_getcatelist.action', function(data) {
		if(data && data.rows && data.rows.length) {
			var html = '';
			$.each(data.rows, function(i, r) {
				html += '<option value="' + r.newsCateId + '">' + r.newsCateName + '</option>';
			});
			$('#newsCateId').append(html);
		}
	});
}

function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap : {},
		successMsg : "成功！",
		failMsg : "失败，请重试！",
		success : successCallBack
	});
}

function successCallBack() {
	refresh();
}

/**
 * 初始化表格数据
 */
function initTable() {
	$('#grid').grid({
		title : '',
		pager : true,
		tbar : '#navbar',
		store : {
			url : '/fw/news/news_getlist.action',
			idProperty : 'newsId',
			uploadPagesize : 'rows'
		},
		pageCfg : {
			btnCount : 5
		},
		columns : [
		        {cn:'标题', en:'newsName', width : 100},
		        {cn:'分类', en:'newsCateName', width : 100},
		        {cn:'是否推荐', en:'isRecommend', width : 100, tpl: function(v) {
		        	if(v.isRecommend)
		        		return '<input type="checkbox" checked="checked" onclick="isRecommendClick(' + v.newsId + ',false)">';
		        	else
		        		return '<input type="checkbox" onclick="isRecommendClick(' + v.newsId + ',true)">';
		        }},
		        {cn:'发布时间',en:'pubTime',width:150, tpl:function(v){
					return formatTime(v.pubTime);
				}},
		        {cn:'列表图片',en:'imgUrl',width:150, tpl:function(v){
					if(v.imgUrl){
						return "<img src='"+v.imgUrl+"' style='width: 100px; height: 75px;'/>";
					}
				}},
		        {cn:'分享图片',en:'shareImgUrl',width:150, tpl:function(v){
					if(v.shareImgUrl){
						return "<img src='"+v.shareImgUrl+"' style='width: 75px; height: 75px;'/>";
					}
				}},
		        {cn:'关注名称',en:'attentionName',width:150},
		        {cn:'关注链接',en:'attentionUrl',width:150, tpl:function(v){
					var html = "暂无";
					if(v.attentionUrl){
						html = v.attentionUrl+"<br/>";
						html += '<button class="btn btn-xs btn-primary" type="button" onclick="javascript:window.open(\''+v.attentionUrl+'\')">点击跳转</button>';
					}
					return html;
				}},
		        {cn:'数据统计', en:'readNum', width : 150, tpl: function(v) {
					var html = '';
					html += '阅读量：';
					if(v.readNum)
						html += v.readNum;
					else
						html += 0;
					html += '<br/>分享量：';
					if(v.shareNum)
						html += v.shareNum;
					else
						html += 0;
					return html;
		        }},
				{cn:'视频链接',en:'videoUrl',width:200, tpl:function(v){
					var html = "暂无";
					if(v.videoUrl){
						if(v.videoUrl.length > 50)
							html = v.videoUrl.substr(0, 50) + "......<br/>";
						else
							html = v.videoUrl + "<br/>";
						html += '<button class="btn btn-xs btn-primary" type="button" onclick="javascript:window.open(\''+v.videoUrl+'\')">点击跳转</button>';
					}
					return html;
				}},
				{cn:'更多',en:'moreSortOrder',width:100, tpl:function(v){
					var html;
					if(v.moreSortOrder > 0){
						html = '<button class="btn btn-xs btn-primary" type="button" onclick="moreout(\'' + v.newsId + '\')">移出更多</button>';
					} else {
						html = '<button class="btn btn-xs btn-primary" type="button" onclick="morein(\'' + v.newsId + '\')">移入更多</button>';
					}
					return html;
				}}
				]
	});
}

function isRecommendClick(newsId, isRecommend) {
	$(this)[0].checked = isRecommend;
	$.getJSON('/fw/news/news_recommend.action', {
		'newsId': newsId
	});
}

function formatTime(t) {
	if(t && t.length >= 14)
		return t.substr(0, 4) + '-' + t.substr(4, 2) + '-' + t.substr(6, 2) + ' ' + t.substr(8, 2) + ':' + t.substr(10, 2) + ':' + t.substr(12, 2);
	return t;
}

function add() {
	location.href = "newsAdd.jsp";
}

function edit(){
	var ids = $('#grid').grid('getSelectedIds');
	if(ids.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要修改的数据!',timer:2000});
		return;
	}else{
		var idsStr = ids[0];
		location.href = "newsEdit.jsp#"+idsStr;
	}
}

function del(){
	var ids = $('#grid').grid('getSelectedIds');
	if(ids.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要删除的数据!',timer:2000});
		return;
	}else{
		var idsStr = ids.join(',');
		if(confirm("这些数据确定要删除数据？")){
			$.getJSON(
				'/fw/news/news_del.action',
				{
					'ids':idsStr
				},
				function(data){
					$('#alertdiv').savehandle('handle',data,2000);
					refresh();
				}
			);
		}
	}
}

function up() {
	updowntop('up');
}

function down() {
	updowntop('down');
}

function btn_top() {
	updowntop('top');
}

function morein(id) {
	updowntop('morein', id);
}

function moreout(id) {
	updowntop('moreout', id);
}

function updowntop(type, id) {
	if(!id) {
		var ids = $('#grid').grid('getSelectedIds');
		if(ids.length == 1) {
			id = ids[0];
		} else if(ids.length == 0){
			$('#alertdiv').savehandle('setmsg',{msg:'请选择一条数据!',timer:2000});
			return;
		} else {
			$('#alertdiv').savehandle('setmsg',{msg:'只能选择一条数据!',timer:2000});
			return;
		}
	}
	$.getJSON('/fw/news/news_' + type + '.action', {
		'newsId':id
	}, function(data) {
		$('#alertdiv').savehandle('handle',data,2000);
	});
}

function search() {
	var data = {};
	var newsCateId = $('#newsCateId').val();
	if(newsCateId && newsCateId > 0)
		data["newsCate.newsCateId"] = newsCateId;
	var newsName = $('#newsName').val();
	if(newsName)
		data["newsName"] = newsName;
	$('#grid').grid('load', {data : data});
}

/**
 * 刷新
 * 
 * @return {TypeName}
 */
function refresh() {
	$('#grid').grid('load', {data : {}});
}

function findAll() {
	refresh();
}